#! /usr/bin/python
import os, string, sys

class AwPlus:
    def __init__(self):
        self.actual_dir = os.getcwd()
        self.trunk_dir = self.actual_dir + '/trunk'
        home = os.getenv('HOME')
        awplus_ext_name = '{a3720d7a-d73f-4453-b889-707f560794d3}'

        if sys.platform == 'linux2':
          ff_dir = home + '/.mozilla/firefox'
        elif sys.platform == 'darwin':
          ff_dir = home + '/Library/Application Support/Firefox/Profiles'
        else:
          print 'Platform not supported!'
          sys.exit()
           
        self.awplus_ext_dir = self.find_ext_dir(ff_dir, awplus_ext_name)


    def find_ext_dir(self, dir_name, find_str):
        directory = False
        for file in os.listdir(dir_name):
            dirfile = os.path.join(dir_name, file)
            # recursively access file names in subdirectories
            if not os.path.isdir(dirfile): continue
            if string.find(dirfile, find_str) > -1:
                directory = dirfile
                break
            else:
                directory = self.find_ext_dir(dirfile, find_str)
                if directory: break

        return directory


    def get_file_list(self, dir):
        file_list = []
        for root, dirs, files in os.walk(dir):
            if root.find('.svn') > 0: continue
            for name in files:
                file_list.append(os.path.join(root, name).replace(dir, ''))
        return file_list


    def sync_files(self):
        trunk = self.get_file_list(self.trunk_dir)
        ext   = self.get_file_list(self.awplus_ext_dir)
        # Create files
        clist = list(set(trunk).difference(set(ext)))
        for cname in clist:
            print 'Creating file "%s"' % cname
            f = open("%s%s" % (self.awplus_ext_dir, cname), 'w+')
            f.close()
        
        # Delete files
        dlist = list(set(ext).difference(set(trunk)))
        for dname in dlist:
            print 'Deleting file "%s"' % dname
            os.remove("%s%s" % (self.awplus_ext_dir, dname))


    def build_ff_diff(self):
        os.chdir(self.awplus_ext_dir)
        print "Building diff"
        os.system('diff -ru . %s > patchff' % self.trunk_dir)
        os.rename('patchff', self.actual_dir + '/patchff')
        os.chdir(self.actual_dir)

    
    def build_svn_diff(self):
        os.chdir(self.trunk_dir)
        print "Building diff"
        os.system('diff -ru . %s > patchsvn' % self.awplus_ext_dir)
        os.rename('patchsvn', self.actual_dir + '/patchsvn')
        os.chdir(self.actual_dir)
        print 'Diff built. (%s/patchsvn)' % self.actual_dir 

    
    def update_ff(self):
        self.sync_files()
        self.build_ff_diff()
        os.rename('patchff',  self.awplus_ext_dir + '/patchff')
        os.chdir(self.awplus_ext_dir)
        print 'Patching...'
        os.system('patch -p0 < patchff')
        os.remove('patchff')
        print 'done!'
        os.chdir(self.actual_dir)


    def make_release(self, version):
        vdir = self.actual_dir + '/tags/' + version;
        if not os.path.isdir(vdir):
            print "SVN tag %s was not found!" % version
            sys.exit()

        os.chdir(vdir)
        os.system('zip -r "awplus-%s.xpi" * -x *.svn*' % version)
        os.rename('awplus-%s.xpi' % version,  self.actual_dir + '/awplus-%s.xpi' % version)
        os.chdir(self.actual_dir)
        print "Done!"


awplus = AwPlus()

cmd = sys.argv[1]

if cmd == 'buildff': awplus.build_ff_diff()
elif cmd == 'updateff': awplus.update_ff()
elif cmd == 'buildsvn': awplus.build_svn_diff()
elif cmd == 'release':
    version = sys.argv[2]
    awplus.make_release(version)
else:
    print "Usage: manager.py <buildff|updateff|buildsvn|release <version> >"
